package com.gxar.quick.ar.server.base.model.works.mapper;

import com.alibaba.fastjson.JSON;
import com.gxar.quick.ar.server.base.model.works.vo.WorksTemplateIntroductionVO;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksTemplateCreateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksTemplateUpdateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateCreateResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateInfoResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateListResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksTemplateUpdateResponse;
import com.gxar.quick.ar.server.db.model.WorksTemplateEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 作品模板转换工具
 *
 * @author Linsy
 * @version 1.0
 * @created 2023/11/8 10:45 AM
 */
@Mapper
public interface WorksTemplateMapStruct {
    WorksTemplateMapStruct INSTANCE = Mappers.getMapper(WorksTemplateMapStruct.class);

    /**
     * 作品模板实体转列表
     *
     * @param entity 实体
     * @return 响应
     */
    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    WorksTemplateListResponse entityToListResponse(WorksTemplateEntity entity);

    /**
     * 作品模板实体转列表
     *
     * @param entities 实体
     * @return 响应
     */
    List<WorksTemplateListResponse> entityToListResponses(List<WorksTemplateEntity> entities);

    /**
     * 作品模板实体转详情
     *
     * @param entity 实体
     * @return 详情
     */
    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    WorksTemplateInfoResponse entityToInfoResponse(WorksTemplateEntity entity);

    @Mapping(target = "introduction", expression = "java(convertIntroduction(request.getIntroduction()))")
    WorksTemplateEntity createToEntity(WorksTemplateCreateRequest request);

    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    WorksTemplateCreateResponse entityToCreate(WorksTemplateEntity entity);

    @Mapping(target = "introduction", expression = "java(convertIntroduction(request.getIntroduction()))")
    WorksTemplateEntity updateToEntity(WorksTemplateUpdateRequest request);

    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    WorksTemplateUpdateResponse entityToUpdate(WorksTemplateEntity entity);

    default String convertIntroduction(WorksTemplateIntroductionVO introduction){
        if (introduction != null) {
            return JSON.toJSONString(introduction);
        }
        return null;
    }
}
